package org.example.medical.service.impl;


import org.example.medical.Mapper.*;
import org.example.medical.entity.Patient;
import org.example.medical.service.PatientService;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    AdmissionRecordMapper admissionRecordMapper;
    @Autowired
    DiagnosisRecordMapper diagnosisRecordMapper;
    @Autowired
    SurgeryRecordMapper surgeryRecordMapper;
    @Autowired
    VisitRecordMapper visitRecordMapper;
    // 分页查询
    @Override
    public PageResult<Patient> getPatients(int page, int size, String name, String gender) {
        int offset = (page - 1) * size;
        List<Patient> list = patientMapper.selectByPage(name, gender, offset, size);
        Long total = patientMapper.countByCondition(name, gender);
        return new PageResult<>(list, total, page, size);
    }

    @Override
    // 动态更新
    public void updatePatient(Patient patient) {
        if (patient.getPatientId() == null) {
            throw new IllegalArgumentException("Patient ID cannot be null");
        }
        patientMapper.updatePatientSelective(patient);
    }

    @Override
    // 软删除
    public void deletePatient(Integer patientId) {
        List<Long> recordIds = admissionRecordMapper.getRecordIdsByPatientId(patientId);
        List<Long> diagnosis = diagnosisRecordMapper.getDiagnosisIdsByPatientId(patientId);
        List<Long> surgeryIds = surgeryRecordMapper.getSurgeryIdsByPatientId(patientId);
        List<Long> visitIds = visitRecordMapper.getRecordIdsByPatientId(patientId);
        if (visitIds != null && !visitIds.isEmpty()){
            visitRecordMapper.batchSoftDeleteVisitRecords(visitIds);
        }
        if (surgeryIds != null && !surgeryIds.isEmpty()){
            surgeryRecordMapper.batchSoftDeleteSurgeryRecords(surgeryIds);
        }
        if (diagnosis != null && !diagnosis.isEmpty()){
            diagnosisRecordMapper.batchSoftDeleteDiagnosisRecords(diagnosis);
        }
        if (recordIds != null && !recordIds.isEmpty()){
            admissionRecordMapper.batchSoftDeleteAdmissionRecords(recordIds);
        }
        patientMapper.softDelete(patientId);
    }

    @Override
    public void addPatient(Patient patient) {
        patientMapper.insertPatient(patient);
    }

    @Override
    public void batchDeletePatients(List<Integer> patientIds) {
        for (Integer patientId : patientIds){
            List<Long> recordId = admissionRecordMapper.getRecordIdsByPatientId(patientId);
            List<Long> diagnosis = diagnosisRecordMapper.getDiagnosisIdsByPatientId(patientId);
            List<Long> surgeryIds = surgeryRecordMapper.getSurgeryIdsByPatientId(patientId);
            List<Long> visitIds = visitRecordMapper.getRecordIdsByPatientId(patientId);
            if (visitIds != null && !visitIds.isEmpty()){
                visitRecordMapper.batchSoftDeleteVisitRecords(visitIds);
            }
            if (surgeryIds != null && !surgeryIds.isEmpty()){
                surgeryRecordMapper.batchSoftDeleteSurgeryRecords(surgeryIds);
            }
            if (diagnosis != null && !diagnosis.isEmpty()){
                diagnosisRecordMapper.batchSoftDeleteDiagnosisRecords(diagnosis);
            }
            if (recordId != null && !recordId.isEmpty()){
                admissionRecordMapper.batchSoftDeleteAdmissionRecords(recordId);
            }
        }
        patientMapper.batchDeletePatients(patientIds);
    }
}
